package com.azkj.album.view.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.azkj.album.R;
import com.azkj.album.dto.MessageEvent;
import com.azkj.album.dto.MessageType;
import com.azkj.album.network.NetworkMaster;
import com.azkj.album.network.callback.ServiceCallback;
import com.azkj.album.network.networkframe.bean.BaseResp;
import com.azkj.album.network.networkframe.net.exception.ApiException;
import com.azkj.album.network.utils.ToastUtils;
import com.azkj.album.utils.DensityUtils;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class FeedDialog {


    public static final class Builder {

        private Context mContext;
        private final Dialog dialog;

        @BindView(R.id.et_desc)
        EditText mEtDesc;

        @BindView(R.id.iv_add)
        ImageView iv_add;

        private String imageUrl;


        public Builder(Context context) {
            this.mContext = context;
            View view = LayoutInflater.from(context).inflate(
                    R.layout.dialog_feed, null);
            ButterKnife.bind(this, view);
            EventBus.getDefault().register(this);

            // 定义Dialog布局和参数
            dialog = new Dialog(context, R.style.MyDialog);
            dialog.setContentView(view);
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.x = 0;
            lp.y = 0;
            lp.width = (int) (DensityUtils.getScreenWidth(context));
            lp.height = (int) (DensityUtils.getScreenHeight(context));
            lp.gravity = Gravity.CENTER;
            dialogWindow.setAttributes(lp);
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);

        }


        public Builder show() {
            dialog.show();
            return this;

        }


        @OnClick({R.id.iv_add, R.id.tv_cancel, R.id.tv_confirm})
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.iv_add:
                    EventBus.getDefault().post(new MessageEvent(MessageType.TYPE_ON_FEED_UPLOAD_CLICK));
                    break;
                case R.id.tv_cancel:
                    dialog.dismiss();

                    break;
                case R.id.tv_confirm:

                    feedback();

                    break;

            }
        }

        private void feedback() {

//            if (TextUtils.isEmpty(imageUrl)) {
//                ToastUtils.showCenterLongToast("请上传图片");
//                return;
//            }
            if (TextUtils.isEmpty(mEtDesc.getText())) {
                ToastUtils.showCenterLongToast("填写描述");
                return;
            }

            NetworkMaster.getInstance().getCommonService().addFeedback(imageUrl, mEtDesc.getText().toString(), new ServiceCallback<BaseResp>() {
                @Override
                public void onSuccess(BaseResp jsonObj) {
                    DialogHelper.hideDialog();
                    if (jsonObj.getCode() == 1) {
                        ToastUtils.showCenterLongToast("提交成功");
                        dialog.dismiss();
                    } else {
                        ToastUtils.showCenterLongToast(jsonObj.getMsg());
                    }
                }

                @Override
                public void onError(ApiException apiException) {
                    ToastUtils.showCenterLongToast(apiException.errorInfo.error);
                }

            });


        }

        @Subscribe
        public void onMsg(MessageEvent msg) {

            switch (msg.getType()) {
                case MessageType.TYPE_ON_FEED_UPLOAD_SUCCESS:
                    imageUrl = msg.getMsg();
                    Glide.with(mContext).load(imageUrl).into(iv_add);


                    break;
            }

        }

    }

}
